package kosta.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import kosta.model.board.BoardVO;
import kosta.model.board.QnABoardService;
import kosta.model.board.TechTipBoardService;
import kosta.model.member.MemberVO;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class TechTipBoardController {
	@Resource(name="techTipBoardServiceImpl")
	private TechTipBoardService techTipboardService;
	@Resource(name="qnABoardServiceImpl")
	private QnABoardService qnABoardService;
	
	//리스트보기
	@RequestMapping("/getListboard.do")
	public ModelAndView getListBoard(HttpSession session,BoardVO bvo) throws Exception{
		MemberVO vo = (MemberVO) session.getAttribute("mvo");
		//System.out.println(vo+"mvo");
		bvo.setMemberVO(vo);
		//System.out.println(bvo.getMemberVO()+"con+--------");
		List<BoardVO> ttlist=techTipboardService.getListBoard();
		//System.out.println(ttlist+"------------controller------------------");
		return new ModelAndView("/ttBoard/techtip_list_board", "ttlist",ttlist);
	}
	
	//상세보기
	@RequestMapping("/findByNo.do")
	public ModelAndView findByNo(int no) throws Exception{
		//System.out.println(no+"---no");
		BoardVO bvo=techTipboardService.findByNo(no);
		System.out.println(bvo+"-------bvo findByNo");
		return new ModelAndView("redirect:findByNoNoHit.do?no="+no);
	}
	//상세보기 nohit
	@RequestMapping("/findByNoNoHit.do")
	public ModelAndView findByNoNoHit(HttpServletRequest request, BoardVO bvo) throws Exception{
		int no=Integer.parseInt(request.getParameter("no"));
		//System.out.println(no+"---bvo no findByNoNoHit");
		bvo=techTipboardService.findByNoNoHit(no);
		return new ModelAndView("/ttBoard/techtip_view_board","bvo",bvo);
	}
	//글쓰는페이지가기
	@RequestMapping("/insertTtBoardView.do")
	public String inertTtBoardView(){
		System.out.println("글쓰러 가자!!");
		return "/ttBoard/techtip_write_board";
	}
	
	//글쓰기
	@RequestMapping("/inertTtBoard.do")
	public ModelAndView inertTtBoard(HttpSession session, BoardVO bvo2) throws Exception{
		//System.out.println(bvo2+"-------------------------");
		MemberVO vo = (MemberVO) session.getAttribute("mvo");
		//System.out.println(vo+"------con insert vo-----------");
		bvo2.setMemberVO(vo);
		//System.out.println(bvo2+"---------con insert bvo2");
		techTipboardService.insertBoard(bvo2);
		BoardVO bvo=techTipboardService.findByNoNoHit(bvo2.getNo());
		return new ModelAndView("redirect:findByNoNoHit.do?no="+bvo.getNo());
	}
	
	//삭제하기
	@RequestMapping("/deleteBoard.do")
	public ModelAndView deleteBoard(int no,String id) throws Exception{
		//System.out.println(no+"---------con-----------------");
		//System.out.println("deleteBoard");
		techTipboardService.deleteBoard(no,id);
		return new ModelAndView("redirect:ttboard.do");
	}
	
	//수정페이지가기
		@RequestMapping("/updateTtBoardView.do")
		public ModelAndView updateTtBoardView(int no) throws Exception{
			BoardVO bvo=techTipboardService.findByNoNoHit(no);
			return new ModelAndView("ttBoard/techtip_update_board","bvo",bvo);
		}
		
		//수정하기
		@RequestMapping("/updateTtBoard.do")
		public ModelAndView updateTtBoard(HttpSession session, BoardVO bvo) throws Exception{
			MemberVO vo = (MemberVO) session.getAttribute("mvo");
			bvo.setMemberVO(vo);
			techTipboardService.updateBoard(bvo);
			bvo=techTipboardService.findByNoNoHit(bvo.getNo());
			return new ModelAndView("ttBoard/techtip_view_board","bvo",bvo);
		}
		
		//검색하기
		@RequestMapping("/find.do")
		public ModelAndView find(HttpSession session,BoardVO bvo, String id,String kind,String find) throws Exception{
			System.out.println(kind+"----kind");
			List<BoardVO> ttlist=null;
			if(kind.equals("id")){
				ttlist=techTipboardService.findById(find);
			}else if(kind.equals("title")){
				ttlist=techTipboardService.findByTitle(find);
			}else if(kind.equals("category")){
				ttlist=techTipboardService.findByCategory(find);
			}
			return new ModelAndView("/ttBoard/ttboard", "ttlist",ttlist);
		}
		
		//menu바에서 찾아가는 메서드
		@RequestMapping("/findByCategory.do")
		public ModelAndView findByCategory(HttpSession session,BoardVO bvo, String id,String kind,String category) throws Exception{
			List<BoardVO> ttlist=techTipboardService.findByCategory(category);
			return new ModelAndView("/ttBoard/ttboard", "ttlist",ttlist);
		}
		
		//잘못게시된 게시물 옮기기(관리자만 가능)
		@RequestMapping("/moveBorad.do")
		public ModelAndView moveBorad(HttpSession session,int no,String id) throws Exception{
			System.out.println(no+"moveBoard----no----");
			BoardVO bvo=techTipboardService.findByNoNoHit(no);
			System.out.println(bvo+"----bvo----");
			techTipboardService.deleteBoard(no,id);
			System.out.println(bvo+"------------------------deleteBoard ok-");
			qnABoardService.moveBoard(bvo);
			return new ModelAndView("redirect:ttboard.do");
		}
}
